<?php


namespace App\Http\Controllers\Common;


use App\Http\Controllers\Controller;
use App\Services\BaseService;
use App\Utils\Code;
use Illuminate\Http\Request;

class BaseController extends Controller
{

    /**
     * 代码自动部署
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function synchronize(Request $request) {
        $pwd = 'jofi12379jadsj0914j9';
        $params = file_get_contents('php://input');
        $params = json_decode($params, true);
        if ((isset($params['password']) && $params['password'] == $pwd) || $request->input('pwd') == $pwd) {
            // 拉取代码
            $command = "sudo -u root git pull origin {$request->input('branch', 'master-lend')} 2<&1";
            exec($command, $output);
//                $res = json_decode(json_encode($output), true);
//                foreach ($res as $item) {
//                    dump("===【 '.$item.'】===");
//                }
            // 生成文档
            // $root = '/www/wwwroot/www.pysfeicui.com/';
            // $command = "cd {$root} && apidoc -i app/Http/Controllers/Api/ -i app/Http/Controllers/Common/ -o public/api-doc";
            // exec($command, $output);
            return json_decode(json_encode($output), true);
//                $res = json_decode(json_encode($output), true);
//                foreach ($res as $item) {
//                    dump("===【 '.$item.'】===");
//                }
        } else {
            throw new \Exception('通讯密钥不正确', Code::PARAMETER_ERROR);
        }
    }

    /**
     * 自动打包-部署
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public function synchronizeVue(Request $request) {
        ini_set('max_execution_time', '0');
        $pwd = 'jofi12379jadsj0914j9';
        $params = file_get_contents('php://input');
        $params = json_decode($params, true);
        if ((isset($params['password']) && $params['password'] == $pwd) || $request->input('pwd') == $pwd) {
            // 进文件夹
            $root = "/www/wwwroot/manager.pysfeicui.com/vue.zhubao.com";
            // 拉取代码
            $command = "cd {$root} && sudo -u root git pull origin {$request->input('branch', 'develop-lend')} 2<&1";
            exec($command, $output);
            // 打包代码
            $command = "cd {$root} && sudo npm run build:prod";
            exec($command, $output);
//            $res = json_decode(json_encode($output), true);
//            foreach ($res as $item) {
//                dump("===【 '.$item.'】===");
//            }
        } else {
            throw new \Exception('通讯密钥不正确', Code::PARAMETER_ERROR);
        }
        return json_decode(json_encode($output), true);
    }

    /**
     * @api {post} new/notice 07、通知轮循
     * @apiName CommonNewNotice
     * @apiGroup 99-Common
     * @apiVersion 1.0.0
     *
     * @apiSuccess {Object} data 返回数据集
     *
     * @param Request $request
     * @param BaseService $service
     * @return array
     */
    public function newNotice(Request $request, BaseService $service) {
        return $service->newNotice($request->input());
    }
}
